﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace BELT
{
    public class BrowserElement
    {
        public static string GetDescription(Object obj) {
            Type objType = obj.GetType();
            string tagName = ((string)(objType.InvokeMember("tagName", BindingFlags.GetProperty, null, obj, null))).ToLower();
            switch (tagName) { 
                case "a":
                    return GetPropertyString(obj, "innerText", "href");
                case "img":
                    return GetPropertyString(obj, "alt", "src");
                case "input":
                    return GetPropertyString(obj, "type", "name", "value");
                case "select":
                    return GetPropertyString(obj, "name");
                default:
                    return "";
            }
        }
        public static string GetPropertyString(Object obj, params string[] args){
            List<string> results = new List<string>();
            Type objType = obj.GetType();
            foreach (string propertyName in args) {
                string oneResult = propertyName + "=";
                try
                {
                    string propertyValue = (string)(objType.InvokeMember(propertyName, BindingFlags.GetProperty, null, obj, null));
                    oneResult += propertyValue;
                }
                catch { 
                    
                }
                results.Add(oneResult);
            }
            return String.Join(", ", results.ToArray());
        }
    }
}
